package hu.radai.jsfportal.taglib;

import java.io.IOException;
import java.util.Map;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.faces.render.FacesRenderer;
import javax.faces.render.Renderer;

@FacesRenderer(componentFamily = "Menu", rendererType = "DemoMenu")
public class MenuRender extends Renderer {

	public MenuRender() {
	}
	
	// Renderer Methods
    /**
     * <p>No decoding is required.</p>
     *
     * @param context
     * <code>FacesContext</code>for the current request
     * @param component
     * <code>UIComponent</code> to be decoded
     */
    @Override
    public void decode(FacesContext context, UIComponent component) {
        if ((context == null) || (component == null)) {
            throw new NullPointerException();
        }
    }

    /**
     * <p>No begin encoding is required.</p>
     *
     * @param context
     * <code>FacesContext</code>for the current request
     * @param component
     * <code>UIComponent</code> to be decoded
     */
    @Override
    public void encodeBegin(FacesContext context, UIComponent component) throws IOException {
        if ((context == null) || (component == null)) {
            throw new NullPointerException();
        }
    }

    /**
     * <p>No children encoding is required.</p>
     *
     * @param context
     * <code>FacesContext</code>for the current request
     * @param component
     * <code>UIComponent</code> to be decoded
     */
    @Override
    public void encodeChildren(FacesContext context, UIComponent component) throws IOException {
        if ((context == null) || (component == null)) {
            throw new NullPointerException();
        }
    }

    /**
     * <p>Encode this component.</p>
     *
     * @param context
     * <code>FacesContext</code>for the current request
     * @param component
     * <code>UIComponent</code> to be decoded
     */
    @Override
    public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
        if ((context == null) || (component == null)) {
            throw new NullPointerException();
        }

        UIMenu uiMenu = (UIMenu) component;
        
        ResponseWriter writer = context.getResponseWriter();
        
    	writer.startElement("div", uiMenu);
    	
    	writer.writeAttribute("id", uiMenu.getClientId(context), "id");
    	
    	writer.writeAttribute("lang", uiMenu.getLang(), "lang");
    	
    	Map<String, String> menu = (Map<String, String>) uiMenu.getMenu();
    	if (menu == null)
    		throw new NullPointerException("menu");
    	if (!menu.isEmpty()) {
    		
    		for(String menuKey : menu.keySet()) {
    			
    			writer.startElement("div", null);
    			writer.writeAttribute("id", menuKey, "id");
    			writer.write((String) menu.get(menuKey));
    			writer.endElement("div");
    		}
    	}
    	
    	writer.endElement("div");
    }

    /**
     * <p>Return the calculated name for the hidden input field.</p>
     *
     * @param context Context for the current request
     * @param component Component we are rendering
     */
    private String getName(FacesContext context, UIComponent component) {
        while (component != null) {
            if (component instanceof UIMenu) {
                return (component.getId());
            }
            component = component.getParent();
        }
        throw new IllegalArgumentException();
    }

    /**
     * <p>Return the path to be passed into JavaScript for the specified
     * value.</p>
     *
     * @param context Context for the current request
     * @param value Partial path to be (potentially) modified
     */
    private String getURI(FacesContext context, String value) {
        if (value.startsWith("/")) {
            return (context.getExternalContext().getRequestContextPath() + value);
        } else {
            return (value);
        }
    }
}
